package com.clwl.supervise.superviseapp.controller.base;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.clwl.supervise.superviseapp.entity.base.TcCompany;
import com.clwl.supervise.superviseapp.entity.customer.DlBertAnswer;
import com.clwl.supervise.superviseapp.entity.customer.DlDataLabelTable;
import com.clwl.supervise.superviseapp.entity.customer.JgCustomerHotMsg;
import com.clwl.supervise.superviseapp.entity.customer.TsCustomerService;
import com.clwl.supervise.superviseapp.entity.result.ResultEntity;
import com.clwl.supervise.superviseapp.entity.risk.JgDynamicScoring;
import com.clwl.supervise.superviseapp.service.base.CustomerServiceService;
import com.clwl.supervise.superviseapp.util.ChatServiceResult;
import com.clwl.supervise.superviseapp.util.bigdata.BigDataUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 智能客服
 */
@RestController
@RequestMapping("customerService")
public class CustomerServiceController {


    @Resource
    private CustomerServiceService  customerServiceService;



    /**
     * 聊天界面发送的消息
     * @param message 消息内容
     * @param pageNum 获取问题第几页
     * @param pageSize 每页数量
     * @return
     */
//    @PostMapping("sendMessage")
//    private ResultEntity<List<TsCustomerService>> sendMessage(@RequestParam(required = true) String message, @RequestParam(required = false,defaultValue = "1") Integer pageNum , @RequestParam(required = false,defaultValue = "5") Integer pageSize){
//        try {
//            //List<TsCustomerService> service = customerServiceService.sendMessage(message,pageNum,pageSize);
//            List<BigDataUtil.Semantics> labelList = customerServiceService.getBertLabelList(message);
//            List<Map<String,String>> tableNameList = customerServiceService.getQueryTableListByBart(message,labelList);
//            List<TsCustomerService> service =  customerServiceService.getMessageResult(message,tableNameList,pageNum,pageSize);
//            return  ResultEntity.success(service);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResultEntity.error("消息发送失败");
//        }
//    }
    
/*
    @PostMapping("sendMessage")
    private ResultEntity<List<TsCustomerService>> sendMessage(@RequestParam(required = true) String message, @RequestParam(required = false,defaultValue = "0") Integer pageNum , @RequestParam(required = false,defaultValue = "5") Integer pageSize){
        try {
            List<BigDataUtil.Semantics> labelList = customerServiceService.getBertLabelList(message);
            List<Map<String,String>> tableNameList = customerServiceService.getQueryTableListByBartN(labelList);
        	List<TsCustomerService> service =  customerServiceService.getMessageResultNew(tableNameList,pageNum,pageSize);
            return  ResultEntity.success(service);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("消息发送失败");
        }
    }*/
    
    
    /**
     * 智能客服新流程-生产环境对接（通过客户发送的消息获取bert中的label）
     * @param message
     * @return 返回机器学习分析后的语句词汇
     */
    @PostMapping("sendMessageNew")
    private ResultEntity sendMessageNew(@RequestParam(required = true) String message,String historyMessage){
        try {
            List<BigDataUtil.Semantics> labelList = new ArrayList<>();//customerServiceService.getBertLabelList(message);
            if(labelList.size() == 0){
                ChatServiceResult result  = customerServiceService.sendLangChain(message, historyMessage);
                if(result == null || StringUtils.isEmpty(result.getMessage())){
                    return ResultEntity.success("");
                }
                List<TsCustomerService> service =  new ArrayList<>();
                TsCustomerService  customerService = new TsCustomerService();
                customerService.setCreateTime(new Date());
                customerService.setReturnTitleName("ggmc");//返回信息标题的字段名
                customerService.setProblemResult(result.getMessage());
                customerService.setProblemAbstract(result.getMessage());
                customerService.setReturnContentName("content");//返回content信息的字段名
                customerService.setProblemTable(result.getMessage());
                customerService.setSendMessage(message);
                customerService.setType("1");
                customerService.setKnowledgeBaseList(result.getKnowledgeBaseList());
                service.add(customerService);
                return  ResultEntity.success(service);
            }
            return  ResultEntity.success(labelList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("消息发送失败");
        }
    }


    /**
     * 通过用户选择的机器学习返回的标签查询 DlBertAnswer表对应 标签的数据
     * @param label
     * @return
     */
    @PostMapping("bertLabelGetAnswerLabel")
    private ResultEntity  bertLabelGetAnswerLabel(@RequestParam(required = true,name = "message") String label,String historyMessage ) {
        try {
            List<DlBertAnswer> tableNameList = customerServiceService.bertLabelGetAnswerLabel(label);
            if(tableNameList.size() == 0){
                ChatServiceResult result  = customerServiceService.sendLangChain(label, historyMessage);
                if(result == null || StringUtils.isEmpty(result.getMessage())){
                    return ResultEntity.success();
                }
                List<TsCustomerService> service =  new ArrayList<>();
                TsCustomerService  customerService = new TsCustomerService();
                customerService.setCreateTime(new Date());
                customerService.setReturnTitleName("ggmc");//返回信息标题的字段名
                customerService.setProblemResult(result.getMessage());
                customerService.setProblemAbstract(result.getMessage());
                customerService.setReturnContentName("content");//返回content信息的字段名
                customerService.setProblemTable(result.getMessage());
                customerService.setSendMessage(label);
                customerService.setType("1");
                customerService.setKnowledgeBaseList(result.getKnowledgeBaseList());
                service.add(customerService);
                return  ResultEntity.success(service);
            }
            return  ResultEntity.success(tableNameList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("消息发送失败");
        }
    }

    /**
     * 智能客服新流程-生产环境对接
     * @return 根据机器学习的语句词汇，返回该词汇对应的数据
     */
    @PostMapping("answerLabelGetComment")
    private ResultEntity  answerLabelGetComment(@RequestParam(required = true,name = "message") String label,String historyMessage  ){
        try {
            List<DlDataLabelTable> tableNameList = customerServiceService.answerLabelGetComment(label);
            if(tableNameList.size() == 0){
                ChatServiceResult result  = customerServiceService.sendLangChain(label, historyMessage);
                if(result == null || StringUtils.isEmpty(result.getMessage())){
                    return ResultEntity.success();
                }
                List<TsCustomerService> service =  new ArrayList<>();
                TsCustomerService  customerService = new TsCustomerService();
                customerService.setCreateTime(new Date());
                customerService.setReturnTitleName("ggmc");//返回信息标题的字段名
                customerService.setProblemResult(result.getMessage());
                customerService.setProblemAbstract(result.getMessage());
                customerService.setReturnContentName("content");//返回content信息的字段名
                customerService.setProblemTable(result.getMessage());
                customerService.setSendMessage(label);
                customerService.setType("1");
                customerService.setKnowledgeBaseList(result.getKnowledgeBaseList());
                service.add(customerService);
                return  ResultEntity.success(service);
            }
            return  ResultEntity.success(tableNameList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("消息发送失败");
        }
    }



    /**
     * 智能客服新流程-生产环境对接
     * @return
     */
    @PostMapping("commentGetLabel")
    private ResultEntity commentGetLabel(@RequestParam(required = true,value = "commentId") String commentId,String historyMessage ){
        try {
            List<Map<String,Object>> serviceList =  customerServiceService.commentGetLabel(commentId);
            if(serviceList.size() == 0){
                ChatServiceResult result  = customerServiceService.sendLangChain("", historyMessage);
                if(result == null || StringUtils.isEmpty(result.getMessage())){
                    return ResultEntity.success();
                }
                List<TsCustomerService> service =  new ArrayList<>();
                TsCustomerService  customerService = new TsCustomerService();
                customerService.setCreateTime(new Date());
                customerService.setReturnTitleName("ggmc");//返回信息标题的字段名
                customerService.setProblemResult(result.getMessage());
                customerService.setProblemAbstract(result.getMessage());
                customerService.setReturnContentName("content");//返回content信息的字段名
                customerService.setProblemTable(result.getMessage());
                customerService.setSendMessage(commentId);
                customerService.setType("1");
                customerService.setKnowledgeBaseList(result.getKnowledgeBaseList());
                service.add(customerService);
                return  ResultEntity.success(service);
            }
            return  ResultEntity.success(serviceList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("消息发送失败");
        }
    }

    /**
     * 最后通过业务获取数据
     * @param label
     * @param tableName
     * @param pageNum
     * @param pageSize
     * @param companyId
     * @return
     */
    @PostMapping("commentLabelGetResult")
    private ResultEntity<List<TsCustomerService>> commentLabelGetResult(@RequestParam(required = true,value = "message") String label,String tableName,@RequestParam(required = false,defaultValue = "1") Integer pageNum , @RequestParam(required = false,defaultValue = "5") Integer pageSize,
                                                                   @RequestParam(required = true,value = "companyId")  String companyId,String  historyMessage){
        try {
            List<TsCustomerService> service =  customerServiceService.getMessageResultNew(tableName,label,pageNum,pageSize,companyId);
            if(service.size() == 0){
                ChatServiceResult result  = customerServiceService.sendLangChain("", historyMessage);
                if(result == null || StringUtils.isEmpty(result.getMessage())){
                    return ResultEntity.success();
                }
                TsCustomerService  customerService = new TsCustomerService();
                customerService.setCreateTime(new Date());
                customerService.setReturnTitleName("ggmc");//返回信息标题的字段名
                customerService.setProblemResult(result.getMessage());
                customerService.setProblemAbstract(result.getMessage());
                customerService.setReturnContentName("content");//返回content信息的字段名
                customerService.setProblemTable(result.getMessage());
                customerService.setSendMessage(label);
                customerService.setType("1");
                customerService.setKnowledgeBaseList(result.getKnowledgeBaseList());
                service.add(customerService);
            }
            return  ResultEntity.success(service);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("消息发送失败");
        }
    }


    /**
     *  获取消息的详细信息
     * @param messageId 消息ID
     * @param tableName 表名
     * @param titleColumnName  标题字段名
     * @param contentColumnName 内容字段名
     * @return
     */
    @GetMapping("getMessageDetail")
    private ResultEntity<Map<String,Object>> getMessageDetail(@RequestParam(required = true) Long messageId, @RequestParam(required = true) String  tableName,
                                                              @RequestParam(required = true) String titleColumnName, @RequestParam(required = true) String contentColumnName){
        try {
             Map<String,Object> service = customerServiceService.getMessageDetail(messageId,tableName,titleColumnName,contentColumnName);
            return  ResultEntity.success(service);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("获取消息内容失败");
        }
    }

    /**
     * 获取猜你想问消息列表(热门问题)
     * @return
     */
    @GetMapping("getHotMessageList")
    private ResultEntity<List<JgCustomerHotMsg>> getHotMessageList(@RequestParam(required = false,defaultValue = "0") Integer pageNum , @RequestParam(required = false,defaultValue = "5") Integer pageSize,String msgTitle){
        try {
           IPage<JgCustomerHotMsg> service = customerServiceService.getHotMessageList(pageNum,pageSize,msgTitle);
            return  ResultEntity.success(service.getRecords());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("获取消息失败");
        }
    }

    /**
     * 热门消息点击次数增加
     * @return
     */
    @PostMapping("addHotMessageViewNum")
    private ResultEntity<List<JgCustomerHotMsg>> addHotMessageViewNum(Long messageId){
        try {
            customerServiceService.addHotMessageViewNum(messageId);
            return  ResultEntity.success( );
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("获取消息失败");
        }
    }

    /**
     * 用户说的语音音频转文字
     * @param file 语音文件wav后缀
     * @return
     */
    @PostMapping("audioToText")
    private ResultEntity audioToText(@RequestParam(value = "file") MultipartFile file ){
        try {
            String service = customerServiceService.audioToText(file);
            return  ResultEntity.success(service);
        } catch (Exception e) {
            return ResultEntity.error(e.getMessage());
        }
    }

    /**
     * 生成没有动态评分但是有评级的动态评分数据
     * @return
     */
    @GetMapping("createDynamicScoring")
    private ResultEntity createDynamicScoring( ){
        try {
            List<JgDynamicScoring> dynamicList = customerServiceService.selectProblemList();
            for (JgDynamicScoring dynamicScoring : dynamicList) {
                TcCompany company = customerServiceService.selectCompanyById(dynamicScoring.getCompanyId());
                //生成动态评分记录
                customerServiceService.createDynamicScoring(dynamicScoring,company);
            }
            return  ResultEntity.success(dynamicList);
        } catch (Exception e) {
            return ResultEntity.error(e.getMessage());
        }
    }


}
